﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.WebsiteConfiguration;
using BCData.WeChatComponent.WeChatMessageRecord;
using BCDto.WeChatComponent.WeChatMessageRecord;
using BCEntity.Common.WebsiteConfiguration;
using BCService.Utils;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BCService.WeChatComponent.WeChatMessageRecordService
{
    /// <summary>
    /// 微信粉丝留言记录服务
    /// </summary>
    public class WeChatMessageRecordService : IWeChatMessageRecordService
    {
        private readonly IConfiguration configuration;
        private readonly IWeChatMessageRecordData weChatMessageRecordData;
        private readonly IWebsiteConfigurationData websiteConfigurationData;
        private readonly string weChatOriginalId;
        private readonly string weChatName;
        public WeChatMessageRecordService(IConfiguration configuration,
            IWeChatMessageRecordData weChatMessageRecordData,
            IWebsiteConfigurationData websiteConfigurationData)
        {
            this.configuration = configuration;
            this.weChatMessageRecordData = weChatMessageRecordData;
            this.websiteConfigurationData = websiteConfigurationData;
            this.weChatOriginalId = configuration["WeChatService:Account"];
            this.weChatName = configuration["WeChatService:WeChatName"];
        }

        public PageableList<WeChatMessageRecordDto> Get(string fromUserName, string toUserName, DateTime? start, DateTime? end, WeChatMessageType? messageType, bool? isReplied, string eventName, string eventKey, bool? ascending, int pageIndex = 1, int pageSize = 10)
        {
            var list = this.weChatMessageRecordData.Get(null,fromUserName, toUserName, messageType, start, end, isReplied, eventName, eventKey, ascending, out int dataCount, pageIndex, pageSize);
            WebsiteConfigurationEntity websiteConfig = websiteConfigurationData.Get(1);
            IEnumerable<WeChatMessageRecordDto> recordList = list.As<IEnumerable<WeChatMessageRecordDto>>();
            foreach (var item in recordList) {
                if (item.FromUserName.Equals(this.weChatOriginalId))
                {
                    item.FromUserNickName = this.weChatName;
                    item.FromUserHeadimgurl = websiteConfig.QuadrateLogo;
                }
                if (item.ToUserName.Equals(this.weChatOriginalId))
                {
                    item.ToUserNickName = this.weChatName;
                    item.ToUserHeadimgurl = websiteConfig.QuadrateLogo;
                }
                if (item.Replied) {
                    var replyList = this.weChatMessageRecordData.Get(item.WeChatMessageRecordId, null, null, null, null, null, null, null, null, null, out int itemDataCount, 1, 99999999);
                    Parallel.ForEach(replyList, replyItem =>
                    {
                        if (replyItem.FromUserName.Equals(this.weChatOriginalId))
                        {
                            replyItem.FromUserNickName = this.weChatName;
                            replyItem.FromUserHeadimgurl = websiteConfig.QuadrateLogo;
                        }
                        if (replyItem.ToUserName.Equals(this.weChatOriginalId))
                        {
                            replyItem.ToUserNickName = this.weChatName;
                            replyItem.ToUserHeadimgurl = websiteConfig.QuadrateLogo;
                        }
                    });
                    item.ReplyMessageList = replyList.As<IEnumerable<WeChatMessageRecordDto>>();
                    item.ReplyMessageCount = itemDataCount;
                }
            }
            return new PageableList<WeChatMessageRecordDto>
            {
                Count = dataCount,
                Items = recordList
            };
        }
    }
}
